package com.system.email.thread;

import com.system.email.InformSend;
import com.system.email.email.EmailInformSend;
import com.system.email.properties.EmailCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

/**
 * @Author hewei
 * @Description //TODO  消息通知发送 多线程发送上下文类
 * @Date 2021/6/22
 **/
@Component
@Slf4j
public class EmailContent implements Runnable{


    private final InformSend informSend;

    private final ExecutorService executorService;

    private final CountDownLatch countDownLatch;


    private EmailMessage emailMessage;

    public EmailContent(EmailInformSend informSend, ExecutorService executorService, CountDownLatch countDownLatch) {
        this.informSend = informSend;
        this.executorService = executorService;
        this.countDownLatch = countDownLatch;
    }


    public void start(String emailAddress, EmailCode emailCode){
        emailMessage.setEmailAddress(emailAddress);
        emailMessage.setEmailType(emailCode.getName(emailCode.getExecKey())+emailAddress);
        executorService.execute(this);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        informSend.send(emailMessage);
    }
}
